define(["jquery","layuiall","jqform",'jq_request','zTree'],function ($,layui,jqform,jq_request,zTree) {
    var form = layui.form;
    controller = {
        index:function () {
            form.on('switch(status)', function(data){
                var status = 0;
                if(data.elem.checked){
                    status = 1
                }
                $.ajax({
                    type:'post',
                    data:{
                        id:data.value,
                        status:status
                    },
                    url:$(data.elem).attr("data-url"),
                    success:function (res) {
                        layer.msg(res.msg)
                    }
                })
            });
            //权限配置
            $(".set-access").click(function () {
                var TreeHtml = '<ul id="MenuTree" class="ztree"></ul>';
                var url = $(this).attr("data-url");
                var datas = JSON.parse($(this).attr("data-list"));
                var rule_id = [];
                var menuTreeAler = layer.open({
                    type: 1,
                    title: "角色选择",
                    content: TreeHtml,
                    area:["300px","350px"],
                    shadeClose:true,
                    btn:["确定"],
                    success: function () {
                        $.ajax({
                            type:"get",
                            url :url,
                            dataType:'json',
                            data:{
                                id:datas.id
                            },
                            success:function (res) {
                                var zTreeObj;
                                var setting = {
                                    view: {
                                        showIcon: true
                                    },
                                    data: {
                                        simpleData: {
                                            enable: true,
                                            idKey:"id",
                                            pIdKey:"parent_id",
                                        },
                                        key:{
                                            name:"title"
                                        }
                                    },
                                    check: {
                                        enable: true,
                                        chkStyle: "checkbox",
                                        radioType: "all"
                                    },
                                    callback: {
                                        onClick: function (e, treeId, treeNode, clickFlag) {
                                            role_id = [];
                                            zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                                            nodes = zTreeObj.getCheckedNodes(true)
                                            for (var i = 0; i < nodes.length; i++) {
                                                rule_id.push(nodes[i].id)
                                            }
                                        },
                                        onCheck:function zTreeOnCheck(event, treeId, treeNode) {
                                            role_id = [];
                                            nodes = zTreeObj.getCheckedNodes(true)
                                            for (var i = 0; i < nodes.length; i++) {
                                                rule_id.push(nodes[i].id)
                                            }

                                        },
                                    }
                                };
                                zTreeObj = $.fn.zTree.init($("#MenuTree"), setting, res);
                                zTreeObj.expandAll(true);
                                var checkedNodes = zTreeObj.getCheckedNodes();
                                for (var i = 0; i < checkedNodes.length; i++) {
                                    rule_id.push(checkedNodes[i].id)
                                }
                            }
                        })
                    },
                    yes:function () {
                        console.log(rule_id)
                        jq_request.post(url,{
                            data:{
                                id:datas.id,
                                rules:rule_id
                            },
                            success:function (res) {
                                layer.msg(res.msg)
                                if(res.code===1){
                                    layer.close(menuTreeAler)
                                }
                            }
                        })
                    }
                })
            })

            jqform.delete()

            //数据全部选中
            form.on('checkbox(select-all)', function(data){

                if(data.elem.checked==true){
                    $("input[name='idList[]']").prop('checked',true)
                }else {
                    $("input[name='idList[]']").prop('checked',false)
                }
                form.render('checkbox');
            });
        },
        addapps:function () {
            jqform.init()
        },
        editapps:function () {
            jqform.init()
        }
    }
    return controller;
})